﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using DAL;

namespace AjaxLibrary
{
    class files : IHttpHandler
    {
        bool IHttpHandler.IsReusable
        {
            get { return true; }
        }
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            string param = context.Request.QueryString["Param"];

            switch (param.ToLower())
            {
                case "uploadfiles":
                   
                    HttpPostedFile filePF = context.Request.Files[0];
                    string msg = "";
                    string error = "";
                    string filename = "";
                    int fileid = 0;
                    if (filePF.ContentLength == 0 || Library.FileLib.isPicByFileName(filePF.FileName) == false)
                        error = "无效图片,请重试";
                    else
                    {
                        FilesEntity file = new FilesEntity();
                        file.SystemName = filename = Library.ImgLib.getUploadFilename(filePF);
                        file.ProductId = 0;
                        file.FileCode = "1";

                        file.ImgName = "";
                        file.IsIndex = false;
                        fileid = logic.files.insert(file);
                        msg = "上传成功";
                    }
                    string result = "{ error:'" + error + "', msg:'" + msg + "',fileid:'" + fileid + "',filename:'" + filename + "'}";

                    context.Response.Output.Write(result);
                    return;
                case "delfile":
                 
                    string delmsg = "删除成功!";
                    string delerror = "";
                    int delfileid = Convert.ToInt32(context.Request.QueryString["fileid"]);
                    try
                    {
                        Library.FileLib.delUploadFile(logic.files.getById(delfileid).SystemName);
                    }
                    catch
                    {
                        
                    }
                    logic.files.delete(delfileid);

                    string delresult = "{ error:'" + delerror + "', msg:'" + delmsg + "'}";

                    context.Response.Output.Write(delresult);
                    return;
                default:
                    context.Response.Output.Write("未知错误，请联系管理员。");
                    return;
            }


        }
    }
}

                